package com.example.wallpaperapp.ui.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wallpaperapp.R;
import com.example.wallpaperapp.adapter.FragmentAdapte;
import com.example.wallpaperapp.base.BaseActivity;
import com.example.wallpaperapp.ui.fragment.FindFragment;
import com.example.wallpaperapp.ui.fragment.HomeFragment;
import com.example.wallpaperapp.ui.fragment.MineFragment;
import com.example.wallpaperapp.utils.StatusBar;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    private FragmentAdapte adapter;
    private BottomNavigationView bottomNavigationView;
    private ViewPager viewPager;
    private TextView bar;
    private long exitTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        StatusBar.SetStatusBar(this.getWindow());
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        //状态栏顶上去
        bar = findViewById(R.id.barHeight);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)bar.getLayoutParams();
        params.height = StatusBar.getStatusBarHeight(this);
        bar.setLayoutParams(params);
        viewPager = findViewById(R.id.viewpager);
        bottomNavigationView = findViewById(R.id.navigation);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new FindFragment());
        fragments.add(new MineFragment());
        adapter = new FragmentAdapte(fragments, getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int menuId = menuItem.getItemId();
                // 跳转指定页面：Fragment
                switch (menuId) {
                    case R.id.navigation_0:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.navigation_1:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.navigation_3:
                        viewPager.setCurrentItem(2);
                        break;
                }
                return false;
            }
        });
        // ViewPager 滑动事件监听
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {}
            @Override
            public void onPageSelected(int i) {
                //将滑动到的页面对应的 menu 设置为选中状态
                bottomNavigationView.getMenu().getItem(i).setChecked(true);
            }
            @Override
            public void onPageScrollStateChanged(int i) {}
        });
    }

    @Override
    protected void initData() {

    }

    //返回两次退出应用
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){
            if(System.currentTimeMillis()-exitTime>1500){
                Toast.makeText(this, "再点击一次退出", Toast.LENGTH_SHORT).show();
                exitTime=System.currentTimeMillis();
            }else{
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        int mess = intent.getIntExtra("message", 0);

    }
}